<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/public/BaseCms.php");

class Admin extends BaseCms
{
    public $tb_name = "admin";

    function __construct()
    {
        parent::__construct($this->tb_name);
    }

//    public static function getInstance(){
//
//        if(!isset(self::$instance)){
//
//            $c = __CLASS__;
//
//            self::$instance = new $c;
//
//        }
//
//        return self::$instance;
//
//    }
    /**
     * 退出
     * @param $token
     */
    public function logout($token)
    {
        session_start();
        $ud = md5($_SESSION["userInfo"]);
        if ($token === $ud) {
            $_SESSION["userInfo"] = null;
            session_destroy();
        }
    }

    /**
     * @param $userName
     * @param $password
     * @param $code
     * @return int  -1 验证码错误,0 用户名或者密码错误 1 认证成功
     */
    public function login($userName, $password, $code)
    {
        session_start();
        $captcha = $_SESSION["captcha"];
        if (empty($userName) || empty($password)) {
            return 0;
        }
        $cond = ["user_name" => $userName];
        if (empty($code) || $captcha !== $code) {
            return -1;
        }

        $data = parent::findOne($cond);
        if (empty($data)) {
            return 0;
        }
        if ($data["password"] === md5($password)) {
            $_SESSION["userInfo"] = $data;
            $data["password"] = null;
            return $data;
        } else {
            return 0;
        }
    }

    /**
     * 获取用户权限
     * @param $userId
     */
    public function permission($userId)
    {

    }

    /**
     * 状态列表
     * @param $statusType
     */
    public function statusList($statusType)
    {
        $rearr = array();
        $arr = ["name" => "正常", "value" => "1"];
        array_push($rearr, $arr);
        $arr = ["name" => "禁用", "value" => "2"];
        array_push($rearr, $arr);
        return $rearr;
    }


}